#include "AppDelegate.h"
#include "basic\BasicScene.h"
#include "scene\MainScene.h"
#include "util\Sound.h"
#include "util\Encryption.h"

USING_NS_CC;

AppDelegate::AppDelegate() {
	Sound::GetInstance();
	Music::GetInstance();

}

AppDelegate::~AppDelegate() 
{
	Sound::DelInstance();
	Music::DelInstance();
	Encryption::DelInstance();
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);
	pEGLView->setDesignResolutionSize(640,960,kResolutionExactFit);
    // turn on display FPS
    pDirector->setDisplayStats(false);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);


	CCUserDefault* saveUser=CCUserDefault::sharedUserDefault();
	if(!saveUser->getBoolForKey("isExist")){

		saveUser->setBoolForKey("isExist",true);
		saveUser->setBoolForKey("music",true);
		saveUser->setBoolForKey("sound",true);
		saveUser->setIntegerForKey("highestscore:",0);
		saveUser->flush();
	}
	if(saveUser->getBoolForKey("music")){
		Music::GetInstance()->setMusicEnable(true);

	}else{
		Music::GetInstance()->setMusicEnable(false);

	}

	if(saveUser->getBoolForKey("sound")){
		Sound::GetInstance()->setSoundEnable(true);

	}else{
		Sound::GetInstance()->setSoundEnable(false);

	}


    // create a scene. it's an autorelease object
    BasicScene *pScene = MainScene::scene();

    // run
    pDirector->runWithScene(pScene);



    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    CCDirector::sharedDirector()->stopAnimation();
	Sound::GetInstance()->pauseAllSound();
	Music::GetInstance()->pauseMusic();
    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    CCDirector::sharedDirector()->startAnimation();
	Sound::GetInstance()->resumeAllSound();
	Music::GetInstance()->resumeMusic();
    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
